package com.zs.studentmanager.controller;

import com.zs.studentmanager.common.ResponseCodeEnum;
import com.zs.studentmanager.common.Result;
import com.zs.studentmanager.entity.domain.GradeClass;
import com.zs.studentmanager.entity.vo.PageVo;
import com.zs.studentmanager.service.GradeClassService;
import com.zs.studentmanager.utils.CommonUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author: 周小白
 * @date: 2024-01-21 18:55
 **/
@RestController
@RequestMapping("/gradeClass")
@Tag(name = "年级班级管理")
public class GradeClassController {

    private final GradeClassService gradeClassService;

    public GradeClassController(GradeClassService gradeClassService) {
        this.gradeClassService = gradeClassService;
    }


    /**
     * 获取年级班级列表
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @param searchValue 搜索关键字
     * @return Result
     */
    @GetMapping("/list")
    @Operation(summary = "获取年级班级列表")
    public Result getGradeClassList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "searchValue", required = false) String searchValue) {
        if (pageNum < 1 || pageSize < 1) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        PageVo<GradeClass> pageVo = gradeClassService.getGradeClassList(pageNum, pageSize, searchValue);

        if (pageVo == null) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success(pageVo);
    }


    /**
     * 添加年级班级
     * @param gradeClass 年级班级信息
     * @return Result
     */
    @PostMapping("/add")
    @Operation(summary = "添加年级班级")
    @Transactional(rollbackFor = Exception.class)
    public Result addGradeClass(@RequestBody GradeClass gradeClass) {
        if (gradeClass == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        long currentUserId = CommonUtils.getCurrentUserId();
        gradeClass.setCreateBy(currentUserId);
        gradeClass.setCreateTime(LocalDateTime.now());

        // 添加
        boolean result = gradeClassService.save(gradeClass);
        if (!result) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }


    /**
     * 获取年级班级详情
     * @param id 年级班级id
     * @return Result
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取年级班级详情")
    public Result getGradeClassDetails(@PathVariable("id") long id) {
        if (id < 1) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        GradeClass gradeClass = gradeClassService.getById(id);
        if (gradeClass == null) {
            return Result.fail("该班级不存在");
        }

        return Result.success(gradeClass);
    }



    /**
     * 删除年级班级
     * @param gradeClass 年级班级信息
     * @return Result
     */
    @PutMapping("/update")
    @Operation(summary = "修改年级班级")
    @Transactional(rollbackFor = Exception.class)
    public Result updateGradeClass(@RequestBody GradeClass gradeClass) {
        if (gradeClass == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        long currentUserId = CommonUtils.getCurrentUserId();
        gradeClass.setUpdateBy(currentUserId);
        gradeClass.setUpdateTime(LocalDateTime.now());

        // 更新
        boolean result = gradeClassService.updateById(gradeClass);
        if (!result) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }





    /**
     * 删除年级班级
     * @param id 年级班级id
     * @return Result
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除年级班级")
    @Transactional(rollbackFor = Exception.class)
    public Result deleteGradeClass(@PathVariable("id") Long id) {
        if (id == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        // 删除
        boolean result = gradeClassService.removeById(id);
        if (!result) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }
}
